package com.example.tabbar.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.tabbar.R
import com.example.tabbar.databinding.FragmentModeBinding


class ModeFragment : Fragment() {
    private var binding : FragmentModeBinding? = null
    private var group: TextView? = null
    private var room: TextView? = null
    private var recipe: TextView? = null
    private var groupFragment: ModeGroupFragment? = null
    private var roomFragment: ModeRoomFragment? = null
    private var recipeFragment: ModeRecipeFragment? = null
    private var currentChildFragment: Fragment? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentModeBinding.inflate(inflater,container,false)
        return binding?.root

    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        group = binding?.group
        room = binding?.room
        recipe = binding?.recipe
        group?.isSelected = true

        setupInitialFragment(savedInstanceState)


        group?.setOnClickListener {
            group?.isSelected = true
            room?.isSelected = false
            recipe?.isSelected = false
            switch(0)
        }
        room?.setOnClickListener {
            group?.isSelected = false
            room?.isSelected = true
            recipe?.isSelected = false
            switch(1)
        }
        recipe?.setOnClickListener {
            group?.isSelected = false
            room?.isSelected = false
            recipe?.isSelected = true
            switch(2)
        }


    }
    private fun setupInitialFragment(savedInstanceState: Bundle?) {
        if (savedInstanceState == null) {
            groupFragment = ModeGroupFragment.newInstance()
            childFragmentManager
                .beginTransaction()
                .add(R.id.mode_content,groupFragment!!,ModeGroupFragment::class.java.simpleName)
                .commit()
            currentChildFragment = groupFragment
        }else {
            groupFragment = childFragmentManager.findFragmentByTag(ModeGroupFragment::class.java.simpleName) as ModeGroupFragment?
            roomFragment = childFragmentManager.findFragmentByTag(ModeRoomFragment::class.java.simpleName) as ModeRoomFragment?
            recipeFragment = childFragmentManager.findFragmentByTag(ModeRoomFragment::class.java.simpleName) as ModeRecipeFragment?

            // 恢复当前显示的fragment
            groupFragment?.let {
                if (!it.isHidden) {
                    currentChildFragment = it
                }
            }

            roomFragment?.let {
                if (!it.isHidden) {
                    currentChildFragment = it
                }
            }
            recipeFragment?.let {
                if (!it.isHidden) {
                    currentChildFragment = it
                }
            }
        }

    }

    private fun switch(tabIndex: Int) {
        var toShowFragment: Fragment? = null
        when (tabIndex) {
            0 -> {
                if (groupFragment == null) {
                    groupFragment = ModeGroupFragment.newInstance()
                }
                toShowFragment = groupFragment
            }
            1 -> {
                if (roomFragment == null) {
                    roomFragment = ModeRoomFragment.newInstance()
                }
                toShowFragment = roomFragment
            }
           2 -> {
                if (recipeFragment == null) {
                    recipeFragment = ModeRecipeFragment.newInstance()
                }
                toShowFragment = recipeFragment
           }
        }

        // 使用事务正确管理 Fragment 显示
        val transaction = childFragmentManager.beginTransaction()

        // 如果当前 Fragment 存在且不是目标 Fragment，则隐藏它
        currentChildFragment?.let {
            if (it != toShowFragment && !it.isHidden) {
                transaction.hide(it)
            }
        }

        // 处理目标 Fragment
        toShowFragment?.let {
            if (!it.isAdded) {
                // 如果 Fragment 还未添加，则添加它
                transaction.add(R.id.mode_content, it, it.javaClass.simpleName)
            } else if(it.isHidden) {
                // 如果 Fragment 已添加，确保它被显示
                transaction.show(it)
            } else {

            }
        }

        transaction.commit()
        currentChildFragment = toShowFragment
    }

    companion object {

        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ModeFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}